package com.itheima.health.controller;

import com.itheima.health.common.PageParam;
import com.itheima.health.common.R;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.model.dtos.AddCheckGroupDto;
import com.itheima.health.model.dtos.UpdateCheckGroupDto;
import com.itheima.health.model.pojos.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @PostMapping("/findPage")
    public ResultPageData getPage(@RequestBody PageParam pageParam){

        ResultPageData resultPageData= checkGroupService.getPage(pageParam);
       return resultPageData;
    }

    /**
     * 新增
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public R addCheckGroup(@RequestBody AddCheckGroupDto dto,@RequestParam("checkitemIds") List<Integer> ids){
        checkGroupService.addCheckGroup(dto,ids);
        R r = new R();
        r.setFlag(true);
        r.setMessage("新增成功");
        return r;
    }
    /**
     * 回显
     */
    @GetMapping("/findById")
    public R getCheckGroupById(Integer id){
        CheckGroup checkGroup = checkGroupService.getCheckGroupById(id);
        R r = new R();
        r.setFlag(true);
        r.setData(checkGroup);
        return r;
    }
    /**
     * 修改
     */
    @PostMapping("/edit")
    public R updateCheckGroupById(@RequestBody UpdateCheckGroupDto dto,@RequestParam("checkitemIds") List<Integer> ids){
        checkGroupService.updateCheckGroupById(dto,ids);
        R r = new R();
        r.setFlag(true);
        r.setMessage("修改成功");
        return r;
    }
    /**
     * 删除
     */
    @DeleteMapping("/deletegroup")
    public R deleteCheckGroupById(Integer id){
        return checkGroupService.deleteCheckGroupById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/findAll")
    public R getCheckGroupAll(){
        List<CheckGroup> checkGroupList=checkGroupService.getCheckGroupAll();
        R r = new R();
        r.setFlag(true);
        r.setData(checkGroupList);
        return r;
    }
    @GetMapping("/findCheckGroupIdsBySetMealId")
    public R getCheckGroupIdsBySetMealId(@RequestParam("SetMealId") Integer id){
       List<Integer> checkGroupList= checkGroupService.getCheckGroupIdsBySetMealId(id);
        R r = new R();
        r.setData(checkGroupList);
        r.setFlag(true);
        return  r;
    }
}
